home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Tricks of the Mac Game Programming Gurus
/
TricksOfTheMacGameProgrammingGurus.iso
/
More Source
/
C⁄C++
/
CopyBitsSpeedTest
/
CopyBitsSpeedTest.c
< prev
next >
Wrap
C/C++ Source or Header
|
1995-03-13
|
5KB
|
188 lines
#include <QDOffScreen.h>
/* Standard inits */
#define kLoopNumber 50
static void InitToolbox(void)
{
InitGraf (&qd.thePort);
InitFonts ();
FlushEvents (everyEvent,0);
InitWindows ();
InitMenus ();
TEInit ();
InitDialogs (nil);
InitCursor ();
} /*InitToolbox*/
void main(void)
{
WindowPtr myWindow;
Rect windowRectangle;
short h,v;
GrafPtr screenPort;
GDHandle screenDevice;
Rect pictRectangle, destRectange;
PicHandle coolPicture;
CTabHandle cTable;
GrafPtr offscreenGWorld = nil;
CGrafPtr idioterIMetrowerks;
// offscreenGWorld is really a GWorldPtr rather than a GrafPtr, but I prefer to
// refer to them by GrafPtrs, since the CopyBits calls get much cleaner.
/* Variables for loops, timing and display */
long beforeTime, afterTime;
long loop;
Str255 tempStr;
InitToolbox();
/* Load the picture*/
coolPicture = GetPicture(128); /*PICT resource #128.*/
if (coolPicture == nil)
{
SysBeep(1);
ExitToShell();
}
/* Get its frame */
pictRectangle = (**coolPicture).picFrame;
/* Move it to 0,0 */
OffsetRect(&pictRectangle, -pictRectangle.left, -pictRectangle.top);
/*Set up the window*/
windowRectangle = pictRectangle;
OffsetRect(&windowRectangle, 64, 64);
windowRectangle.bottom += 100;
myWindow = NewCWindow(0L, &windowRectangle, "\pCopyBitsSpeedTest", true, 0, (WindowPtr)-1L, false, 0);
SetPort(myWindow);
DrawPicture(coolPicture, &pictRectangle);
/* Remember it so we can get back after visiting a GWorld */
GetGWorld((GWorldPtr *)&screenPort, &screenDevice);
/* Create a GWorld to draw the PICT in */
if ( noErr != NewGWorld((GWorldPtr *)&offscreenGWorld, 0, &pictRectangle, nil, nil, 0) )
ExitToShell();
/*We lock the offscreen pixmap so we can CopyBits and PlotCIcon to it.*/
if ( LockPixels(((CGrafPtr)offscreenGWorld)->portPixMap) )
;
SetGWorld((GWorldPtr)offscreenGWorld, nil);
PaintRect(&pictRectangle);
DrawPicture(coolPicture, &pictRectangle);
SetGWorld((GWorldPtr)screenPort, screenDevice);
CopyBits(&offscreenGWorld->portBits, &myWindow->portBits, &pictRectangle, &pictRectangle, srcCopy, nil);
// Aligned, optimal copy
beforeTime = TickCount();
for (loop = 0; loop < kLoopNumber; loop++)
{
EraseRect(&pictRectangle);
CopyBits(&offscreenGWorld->portBits, &myWindow->portBits, &pictRectangle, &pictRectangle, srcCopy, nil);
}
afterTime = TickCount();
MoveTo(10, windowRectangle.bottom - 80 - 64);
NumToString(afterTime - beforeTime, tempStr);
DrawString("\pAligned copy: ");
DrawString(tempStr);
DrawString("\p ticks.");
// Wrong transfer mode
beforeTime = TickCount();
for (loop = 0; loop < kLoopNumber; loop++)
{
EraseRect(&pictRectangle);
CopyBits(&offscreenGWorld->portBits, &myWindow->portBits, &pictRectangle, &pictRectangle, srcOr, nil);
}
afterTime = TickCount();
MoveTo(10, windowRectangle.bottom - 65 - 64);
NumToString(afterTime - beforeTime, tempStr);
DrawString("\psrcOr: ");
DrawString(tempStr);
DrawString("\p ticks.");
// Not longword aligned
destRectange = pictRectangle;
OffsetRect(&destRectange, 1, 0);
beforeTime = TickCount();
for (loop = 0; loop < kLoopNumber; loop++)
{
EraseRect(&pictRectangle);
CopyBits(&offscreenGWorld->portBits, &myWindow->portBits, &pictRectangle, &destRectange, srcCopy, nil);
}
afterTime = TickCount();
MoveTo(10, windowRectangle.bottom - 50 - 64);
NumToString(afterTime - beforeTime, tempStr);
DrawString("\pUnaligned copy: ");
DrawString(tempStr);
DrawString("\p ticks.");
// Not same dimensions
destRectange = pictRectangle;
destRectange.right ++;
beforeTime = TickCount();
for (loop = 0; loop < kLoopNumber; loop++)
{
EraseRect(&pictRectangle);
CopyBits(&offscreenGWorld->portBits, &myWindow->portBits, &pictRectangle, &destRectange, srcCopy, nil);
}
afterTime = TickCount();
MoveTo(10, windowRectangle.bottom - 35 - 64);
NumToString(afterTime - beforeTime, tempStr);
DrawString("\pMismatching dimensions: ");
DrawString(tempStr);
DrawString("\p ticks.");
// Color table mismatch
//myGWorldFlags := UpdateGWorld((GWorldPtr)offscreenGWorld,0,boundsRect,cTable,aGDevice,0);
UnlockPixels(((CGrafPtr)offscreenGWorld)->portPixMap);
DisposeGWorld((GWorldPtr)offscreenGWorld);
cTable = GetCTable(128);
// UpdateGWorld((GWorldPtr *)&offscreenGWorld,4,nil,cTable,nil,0);
/* Create a GWorld to draw the PICT in */
if ( noErr != NewGWorld((GWorldPtr *)&offscreenGWorld, 8, &pictRectangle, cTable, nil, 0) )
ExitToShell();
/*We lock the offscreen pixmap so we can CopyBits and PlotCIcon to it.*/
if ( LockPixels(((CGrafPtr)offscreenGWorld)->portPixMap) )
;
SetGWorld((GWorldPtr)offscreenGWorld, nil);
PaintRect(&pictRectangle);
DrawPicture(coolPicture, &pictRectangle);
SetGWorld((GWorldPtr)screenPort, screenDevice);
beforeTime = TickCount();
for (loop = 0; loop < kLoopNumber; loop++)
{
EraseRect(&pictRectangle);
CopyBits(&offscreenGWorld->portBits, &myWindow->portBits, &pictRectangle, &pictRectangle, srcCopy, nil);
}
afterTime = TickCount();
MoveTo(10, windowRectangle.bottom - 20 - 64);
NumToString(afterTime - beforeTime, tempStr);
DrawString("\pCLUT mismatch: ");
DrawString(tempStr);
DrawString("\p ticks.");
while (!Button());
} /*main*/